package com.edu.etu.gui.checkit;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

public class Template extends ActionBarActivity implements View.OnClickListener{

    ImageButton btnAddElem;
    EditText element;
    LinearLayout llMain;
    SQLiteHelperList openHelperList;
    SQLiteDatabase db;
    ContentValues cv;
    EditText input;
    long rowIdSp;
    Boolean f;
    private HashMap<View,Integer> mapTemplates = new HashMap<View,Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template);

        btnAddElem=(ImageButton) findViewById(R.id.btnAddElem);
        btnAddElem.setOnClickListener(this);

        element = (EditText) findViewById(R.id.Element);
        llMain= (LinearLayout) findViewById(R.id.llMain);

        openHelperList = new SQLiteHelperList(getApplicationContext());
        db = openHelperList.getWritableDatabase();
        cv = new ContentValues();
        template();
        f=true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.template, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        llMain.removeAllViews();
        if(f==true)template();
            switch (v.getId())
            {
                case R.id.btnAddElem:
                    AlertDialog.Builder adb = new AlertDialog.Builder(this);
                    input = new EditText(this);
                    adb.setTitle(R.string.Enter2);
                    adb.setView(input);
                    adb.setNegativeButton(R.string.no, myClickListener);
                    adb.setPositiveButton(R.string.yes, myClickListener);
                    adb.show();
                    break;
                default:
                    for(Map.Entry<View,Integer> entry : mapTemplates.entrySet()) {
                        if(entry.getKey() == v)
                        {
                            Intent intent = new Intent(this, ViewTemplateActivity.class);
                            intent.putExtra("TemplateId",entry.getValue());
                            startActivity(intent);
                        }
                    }
                    break;
            }


    }
    DialogInterface.OnClickListener myClickListener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case Dialog.BUTTON_POSITIVE:
                    if(input.length()==0)input.setText("Template");
                    cv.put(openHelperList.nameListsTemplates, input.getText().toString());
                    rowIdSp = db.insert(openHelperList.NameTableListsTemplate,null,cv);
                    Intent temp = new Intent(getBaseContext(), EditTemplateActivity.class);
                    temp.putExtra("TemplateId",(int)rowIdSp);
                    startActivity(temp);
                    finish();
                    break;
                case Dialog.BUTTON_NEGATIVE:

                    break;
            }
        }
    };
    public void template()
    {
        Cursor cList = db.query("ListsTemplates", null, null, null, null, null, null);
        if (cList.moveToFirst()) {
            do {
                LayoutInflater inflater = getLayoutInflater();
                View main_list_element = inflater.inflate(R.layout.main_list_element,null,false);
                Button open_list_button = (Button)main_list_element.findViewById(R.id.list_open_button);
                mapTemplates.put(open_list_button, cList.getInt(cList.getColumnIndex("_id4")));
                open_list_button.setOnClickListener(this);
                open_list_button.setText(cList.getString(cList.getColumnIndex("nameListsTemplates")));
                llMain.addView(main_list_element);
            }
            while (cList.moveToNext());
        }
        cList.close();
    }
}

